import json

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.urls import reverse

from .models import Node, Edge


def divideChapters(sequence):

    book = sequence[0:3]
    chapter = sequence[3:5]
    summary = sequence[5:7]

    return book, chapter, summary


def makeMenu(node):
    bookMenu = []
    chapterMenu = []
    summaryMenu = []

    for da in node:
        if da['structure']['chapter'] != "00":
            if da['structure']['summary'] != "00":
                summaryMenu.append(da['name'])
            else:
                chapterMenu.append(da['name'])
        else:
            bookMenu.append(da['name'])

    menu = {
        'bookMenu': bookMenu,
        'chapterMenu': chapterMenu,
        'summaryMenu': summaryMenu
    }

    return menu


def get_data_links():
    data = Node.objects.filter(is_delete=False)
    node = []
    maxCategory = 0

    for d in data:
        da = {
            'name': '',
            'value': {'belong_to': '', 'desc': '', },
            'category': 0,
            'identifier': '',
            'structure': {
                'book': '',
                'chapter': '',
                'summary': ''
            },
        }

        # da['id'] = d.id
        da['name'] = d.name
        da['value']['belong_to'] = d.belong_to
        da['value']['desc'] = d.desc
        da['category'] = int(d.category)
        da['identifier'] = d.identifier
        book, chapter, summary = divideChapters(d.identifier)
        da['structure']['book'] = book
        da['structure']['chapter'] = chapter
        da['structure']['summary'] = summary

        if maxCategory < int(d.category):
            maxCategory = int(d.category)

        node.append(da)

    print(node)

    links = Edge.objects.filter(is_delete=False)
    edge = []

    for l in links:
        li = {
            'source': '',
            'target': '',
            'category': 0,
            'value': '',
            'symbolSize': 10
        }

        li['id'] = l.id
        li['source'] = l.source
        li['target'] = l.target
        li['value'] = l.value
        edge.append(li)

    return node, edge, maxCategory


def index(request):
    data, links, maxCategory = get_data_links()

    d1 = json.dumps(data)
    l1 = json.dumps(links)

    menu = makeMenu(data)
    print(menu)

    menuJson = json.dumps(makeMenu(data))

    return render(request, 'graph/index.html', {'data': d1, 'links': l1, 'maxCategory': maxCategory, 'menu': menu, 'menuJson': menuJson})


def delete(request):  # 用来删除结点
    if request.method == 'GET':
        node_name = request.GET.get('node_name').strip()
        de_node = Node.objects.get(name=node_name)
        de_node.is_delete = True
        de_node.save()

        de_edge_source = Edge.objects.get(source=node_name)
        de_edge_source.is_delete = True
        de_edge_source.save()

        de_edge_target = Edge.objects.get(target=node_name)
        de_edge_target.is_delete = True
        de_edge_target.save()
    return HttpResponse("删除成功!")
    # return render(request, 'graph/graph.html', {'data': data, 'links': links})


def add_node(request):  # 用来添加结点
    if request.method == 'GET':
        name = request.GET['add_node_name']
        belong_to = request.GET['add_belong_to']
        desc = request.GET['add_desc']
        identifier = request.GET['add_identifier']
        category = int(request.GET['add_category'])
        # new_node = Node(name=name, belong_to=belong_to, desc=desc, identifier=identifier, category=category)
        # new_node.save()
    return HttpResponse("添加结点成功!")
    # return HttpResponseRedirect('/graph/', args=[])


def add_edge(request):  # 用来添加结点
    if request.method == 'GET':
        source = request.GET['add_source']
        target = request.GET['add_target']
        value = request.GET['add_value']
        new_edge = Edge(source=source, target=target, value=value)
        new_edge.save()
    return HttpResponse("添加关系线成功!")


def update_node(request):  # 用来修改结点信息
    if request.method == 'GET':
        node_name = request.GET['update_node_name']
        belong_to = request.GET['update_belong_to']
        desc = request.GET['update_desc']
        identifier = request.GET['update_identifier']
        category = int(request.GET['update_category'])

        update_node = Node.objects.get(name=node_name)
        update_node.name = node_name
        update_node.belong_to = belong_to
        update_node.desc = desc
        update_node.identifier = identifier
        update_node.category = category

        update_node.save()

    return HttpResponse("修改结点成功!")
    # return HttpResponseRedirect('/graph/', args=[])
